﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Configuration;
using System.IO;
using SalonPay.Entity;
using System.Threading;

namespace SalonPay.BLL
{
    /// <summary>
    /// Socket连接/操作类
    /// 2010/1/23
    /// </summary>
    public class GameSocketConn
    {
        #region 实例
        public static string UTF8toGB2312(string str)
        {
            string utfinfo = str;
            string gb2312info = string.Empty;

            Encoding utf8 = Encoding.UTF8;
            Encoding gb2312 = Encoding.GetEncoding("gb2312");
            byte[] unicodeBytes = utf8.GetBytes(utfinfo);
            byte[] asciiBytes = Encoding.Convert(utf8, gb2312, unicodeBytes);
            char[] asciiChars = new char[gb2312.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
            gb2312.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
            gb2312info = new string(asciiChars);
            return gb2312info;
        }
        public static IPAddress ip
        {
            get { return IPAddress.Parse(ConfigurationManager.AppSettings["TcpIP"].ToString()); }
        }
        public static int Port
        {
            get { return int.Parse(ConfigurationManager.AppSettings["TcpPort"].ToString()); }
        }
        public static byte[] buffer;
        public static byte[] rebuffer;
        public static TcpClient _tcpClient = new TcpClient();
        public static NetworkStream _networkStream;
        /// <summary>
        /// 是否已成功登陆
        /// </summary>
        public static bool INetLoginBoolean = false;
        public static string Username { get { return ConfigurationManager.AppSettings["username"].ToString(); } }
        public static string Password { get { return ConfigurationManager.AppSettings["password"].ToString(); } }
        public GameSocketConn()
        {
            try
            {
                if (_tcpClient == null)
                {
                    _tcpClient = new TcpClient();
                    _tcpClient.Connect(ip, Port);
                }
            }
            catch (Exception ex)
            {
                _tcpClient.Close();
            }
        }
        /// <summary>
        /// 关闭TcpClient连接
        /// </summary>
        public static void OnTcpClientStop()
        {
            _tcpClient.Close();
            _networkStream.Close();
            _networkStream.Dispose();
        }
        #endregion

        #region
        /// <summary>
        /// 
        /// </summary>
        /// <param name="objrebuffer">byte[]数组</param>
        /// <param name="offset">偏离位置</param>
        /// <param name="indexcount">索引总数</param>
        /// <returns></returns>
        public static byte[] Format10Bytes(byte[] objrebuffer, int offset, int indexcount)
        {
            if (objrebuffer != null)
            {
                byte[] msgId = new byte[indexcount];
                for (int i = offset; i < offset + indexcount; i++)
                {
                    msgId.SetValue(objrebuffer[i], i - offset);
                }
                return msgId;
            }
            else
            { return null; }
        }
        #endregion

        #region 多线程操作/回声
        public static bool IsEcho = false;
        public static Timer _timer;
        public static void OnTimerStart()
        {
            _timer = new Timer(new TimerCallback(SNetMsg_Echo), _tcpClient, 8000, 8000);
        }

        public static void OnThreadStop()
        {
            if (_timer != null)
            {
                _timer.Dispose();
                _timer = null;
            }
        }
        #endregion

        #region ECHO_MSG 回声
        public static void SNetMsg_Echo(object data)
        {
            MessageTypeHandler message = new MessageTypeHandler();
            if (!_tcpClient.Connected) { try { _tcpClient.Connect(ip, Port); } catch { Log.WriterLog("1|11001" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "连接服务器失败"); return; } }
            try
            {
                byte[] msgSize = BitConverter.GetBytes((int)12);
                byte[] msgId = BitConverter.GetBytes((int)500);
                byte[] msgDworo = BitConverter.GetBytes(0);
                byte[] msg = new byte[12];
                Buffer.BlockCopy(msgSize, 0, msg, 0, 4);
                Buffer.BlockCopy(msgId, 0, msg, 4, 4);
                Buffer.BlockCopy(msgDworo, 0, msg, 8, 4);
                SendMsg(msg, 32, message);
            }
            catch (StackOverflowException ex)
            {
                MessageTypeHandler.SNetMsg_RegUserLoginResult_MessageType = ex.Message;
            }
        }
        #endregion

        #region Socket发送套接字并同步接收套接字
        public static void SendMsg(byte[] msgContent, int rebbufferlength, MessageTypeHandler message)
        {
            int msgid = BitConverter.ToInt32(Format10Bytes(msgContent, 4, 4), 0);
            if (!_tcpClient.Connected)
            {
                try
                {
                    _tcpClient.LingerState.Enabled = false;
                    _tcpClient.Close();
                    _tcpClient = new TcpClient();
                    _tcpClient.Connect(ip, Port);
                    if (_tcpClient.Connected)
                    {
                        SNetMsg_RegUserLogin(message);
                    }
                }
                catch
                { return; }
            }

            if (_tcpClient.Connected)
            {
                //if (_tcpClient != null)
                //{
                    try
                    {
                        _networkStream = _tcpClient.GetStream();
                        if (_networkStream.CanWrite)
                        {
                            _networkStream.Write(msgContent, 0, msgContent.Length);
                        }
                        else
                        {
                            message.SNetMsg_Boolean = false;
                            message.SNetMsg_SocketException_MessageType = "1|" + msgid + "|Socket数据流不可写";
                            Log.WriterLog(message.SNetMsg_SocketException_MessageType);  
                        }
                        if (msgid != 500)
                        {
                            if (_networkStream.CanRead)
                            {
                                rebuffer = new byte[rebbufferlength];

                                _networkStream.Read(rebuffer, 0, rebuffer.Length);
                            }
                            else
                            {
                                message.SNetMsg_Boolean = false;
                                message.SNetMsg_SocketException_MessageType = "1|" + msgid + "|Socket数据流不可写";
                                Log.WriterLog(message.SNetMsg_SocketException_MessageType);  
                            }
                        }
                    }
                    catch (IOException ex)
                    {
                        message.SNetMsg_Boolean = false;
                        message.SNetMsg_SocketException_MessageType = "1|" + msgid + "|" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "消息ID：" + BitConverter.ToInt32(Format10Bytes(msgContent, 4, 4), 0) + ">>連接Socket失敗原因：" + ex.InnerException;
                        Log.WriterLog(message.SNetMsg_SocketException_MessageType);  
                    }
                    catch (StackOverflowException ex)
                    {
                        message.SNetMsg_Boolean = false;
                        message.SNetMsg_SocketException_MessageType = ex.Source;
                        Log.WriterLog(message.SNetMsg_SocketException_MessageType);  
                    }
                //}
            }
        }
        #endregion

        #region 判斷是否登陸socket
        /// <summary>
        /// 是否登陆Socket
        /// </summary>
        public static void SNetMsg_RegUserLogin(MessageTypeHandler message)
        {
            if (!_tcpClient.Connected) { try { _tcpClient.Connect(ip, Port); } catch { return; } }
            try
            {
                buffer = new byte[40];
                byte[] msgSize = BitConverter.GetBytes((int)40);
                byte[] msgId = BitConverter.GetBytes((int)10001);
                byte[] msgname = new byte[16];
                Encoding.Default.GetBytes(Username, 0, Username.Length, msgname, 0);
                byte[] msgpwd = new byte[16];
                Encoding.Default.GetBytes(Password, 0, Password.Length, msgpwd, 0);
                byte[] msg = new byte[40];
                Buffer.BlockCopy(msgSize, 0, msg, 0, 4);
                Buffer.BlockCopy(msgId, 0, msg, 4, 4);
                Buffer.BlockCopy(msgname, 0, msg, 8, 16);
                Buffer.BlockCopy(msgpwd, 0, msg, 24, 16);
                SendMsg(msg, 32, message);
                SnetMsg_RegUserLoginComplete(message);
            }
            catch (SocketException ex)
            {
                message.SNetMsg_Boolean = false;
                message.SNetMsg_MessageType = "1|10001" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "登陸Socket失败,原因：" + ex.InnerException;
            }
        }

        private static void SnetMsg_RegUserLoginComplete(MessageTypeHandler message)
        {
            try
            {
                int readResult = BitConverter.ToInt32(Format10Bytes(rebuffer, 4, 4), 0);
                if (readResult == 10002)
                {
                    bool readResultbSuccess = BitConverter.ToBoolean(Format10Bytes(rebuffer, 24, 4), 0);
                    int readResulterrorCode = 0; BitConverter.ToInt32(Format10Bytes(rebuffer, 28, 4), 0);
                    if (readResultbSuccess)
                    {
                        message.SNetMsg_MessageType = "0|" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "登陸Socket成功";
                        OnTimerStart();
                    }
                    else
                    {
                        switch (readResulterrorCode)
                        {
                            case 1:
                                message.SNetMsg_MessageType = "1|" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "登陸Socket错误，错误ID>>1：错误内容：帐号重复";
                                break;
                            case 2:
                                message.SNetMsg_MessageType = "1|" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "登陸Socket错误，错误ID>>2：错误内容：帐号不符合";
                                break;
                            case 3:
                                message.SNetMsg_MessageType = "1|" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "登陸Socket错误，错误ID>>3：错误内容：其它错误";
                                break;
                        }
                    }
                }
            }
            catch (IOException ex)
            {
                message.SNetMsg_Boolean = false;
                message.SNetMsg_SocketException_MessageType = "1|" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "登陆返回消息时出错：" + ex.Source;
                Log.WriterLog(message.SNetMsg_SocketException_MessageType);
            }
            catch (Exception ex)
            {
                message.SNetMsg_Boolean = false;
                message.SNetMsg_SocketException_MessageType = "1|" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "登陸Socket错误" + ex.Source;
                Log.WriterLog(message.SNetMsg_SocketException_MessageType);
            }
        }
        #endregion

        #region 检测帐号是否存在
        /// <summary>
        /// 检测贴是否存在
        /// </summary>
        /// <param name="username">用户名</param>
        public static void SNetMsg_RegCardIsExist(string username, MessageTypeHandler message)
        {
            OnThreadStop();
            try
            {
                buffer = new byte[24];
                byte[] msgSize = BitConverter.GetBytes((int)24);
                byte[] msgId = BitConverter.GetBytes((int)10005);
                byte[] msgname = new byte[16];
                Encoding.Default.GetBytes(username, 0, username.Length, msgname, 0);
                byte[] msg = new byte[24];
                Buffer.BlockCopy(msgSize, 0, msg, 0, 4);
                Buffer.BlockCopy(msgId, 0, msg, 4, 4);
                Buffer.BlockCopy(msgname, 0, msg, 8, 16);
                SendMsg(msg, 28, message);
                SNetMsg_RegCardExistResult(message);
            }
            catch (SocketException ex)
            {
                message.SNetMsg_Boolean = false;
                message.SNetMsg_SocketException_MessageType = "1|10005|" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "检测帐号出错" + ex.InnerException;
            }
        }
        private static void SNetMsg_RegCardExistResult(MessageTypeHandler message)
        {
            int resultmsgId = BitConverter.ToInt32(Format10Bytes(rebuffer, 4, 4), 0);
            try
            {
                if (resultmsgId == 10006)
                {
                    string resultCardId = Encoding.Default.GetString(Format10Bytes(rebuffer, 8, 16));
                    bool IsRegCardIsExist = BitConverter.ToBoolean(Format10Bytes(rebuffer, 24, 4), 0);
                    if (IsRegCardIsExist)
                        message.SNetMsg_MessageType = "true|" + resultmsgId + "|" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "检测帐号已存在|" + resultCardId;
                    else
                        message.SNetMsg_MessageType = "false|" + resultmsgId + "|" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "检测帐号不存在" + resultCardId;
                }
            }
            catch (Exception ex)
            {
                message.SNetMsg_Boolean = false;
                message.SNetMsg_SocketException_MessageType = "false|" + resultmsgId + "|" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "检测帐号出现异常" + ex.InnerException;
                Log.WriterLog(message.SNetMsg_SocketException_MessageType);  
            }
            OnTimerStart();
        }
        #endregion

        public static Encoding Gb2312 = Encoding.GetEncoding("gb2312");

        #region 新建立帐号信息
        /// <summary>
        /// 新建立帐号信息
        /// </summary>
        /// <param name="model">用户信息实体类</param>
        public static void SNetMsg_RegCreateCard(tb_UserInfo model, MessageTypeHandler message)
        {
            OnThreadStop();
            try
            {
                byte[] msgSize = BitConverter.GetBytes((int)212);
                byte[] msgId = BitConverter.GetBytes((int)10003);
                byte[] msgcardId = new byte[16];
                Gb2312.GetBytes(model.UserName, 0, model.UserName.Length, msgcardId, 0);
                byte[] msgUserPwd = new byte[16];
                Encoding.Default.GetBytes(model.UserPwd, 0, model.UserPwd.Length, msgUserPwd, 0);
                byte[] msgdraftPass = new byte[16];
                Gb2312.GetBytes(model.UDrawPwd, 0, model.UDrawPwd.Length, msgdraftPass, 0);
                byte[] msgbMale = BitConverter.GetBytes((int)model.UGender == 1 ? 1 : 0);
                byte[] msgcardName = new byte[40];
                Gb2312.GetBytes(UTF8toGB2312(model.UTrueName), 0, UTF8toGB2312(model.UTrueName).Length, msgcardName, 0);
                byte[] msgcountryName = new byte[16];
                Gb2312.GetBytes(UTF8toGB2312(model.UCountry), 0, UTF8toGB2312(model.UCountry).Length, msgcountryName, 0);
                byte[] msgteleType = BitConverter.GetBytes(model.UTeleType);
                byte[] msgteleContent = new byte[40];
                Gb2312.GetBytes(model.UTeleContent, 0, model.UTeleContent.Length, msgteleContent, 0);
                byte[] msgmoneyChip = new byte[4];
                Gb2312.GetBytes(model.UBankType, 0, model.UBankType.Length, msgmoneyChip, 0);
                byte[] msgbankCard = new byte[24];
                Gb2312.GetBytes(model.UBankCard, 0, model.UBankCard.Length, msgbankCard, 0);
                byte[] msgbankName = new byte[20];
                Gb2312.GetBytes(UTF8toGB2312(model.UBankName), 0, UTF8toGB2312(model.UBankName).Length, msgbankName, 0);
                IPAddress ipAddress = IPAddress.Parse(model.URegIp);
                byte[] msgIP = ipAddress.GetAddressBytes();
                byte[] msgIpbytes = new byte[4];
                int p = 0;
                foreach (byte ip in msgIP)
                {
                    msgIpbytes[p] = ip;
                    p++;
                }
                byte[] msg = new byte[212];
                Buffer.BlockCopy(msgSize, 0, msg, 0, 4);
                Buffer.BlockCopy(msgId, 0, msg, 4, 4);
                Buffer.BlockCopy(msgcardId, 0, msg, 8, 16);
                Buffer.BlockCopy(msgUserPwd, 0, msg, 24, 16);
                Buffer.BlockCopy(msgdraftPass, 0, msg, 40, 16);
                Buffer.BlockCopy(msgbMale, 0, msg, 56, 4);
                Buffer.BlockCopy(msgcardName, 0, msg, 60, 40);
                Buffer.BlockCopy(msgcountryName, 0, msg, 100, 16);
                Buffer.BlockCopy(msgteleType, 0, msg, 116, 4);
                Buffer.BlockCopy(msgteleContent, 0, msg, 120, 40);
                Buffer.BlockCopy(msgmoneyChip, 0, msg, 160, 4);
                Buffer.BlockCopy(msgbankCard, 0, msg, 164, 24);
                Buffer.BlockCopy(msgbankName, 0, msg, 188, 20);
                Buffer.BlockCopy(msgIpbytes, 0, msg, 208, 4);
                SendMsg(msg, 32, message);
                SNetMsg_RegCreateCardResult(message);
            }
            catch (SocketException ex)
            {
                message.SNetMsg_Boolean = false;
                message.SNetMsg_SocketException_MessageType = "1|10003|" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "建立帐号失败,原因：" + ex.InnerException;
                Log.WriterLog(message.SNetMsg_SocketException_MessageType);  
            }
        }
        private static void SNetMsg_RegCreateCardResult(MessageTypeHandler message)
        {
            int resultmsgId = BitConverter.ToInt32(Format10Bytes(rebuffer, 4, 4), 0);
            if (resultmsgId == 10004)
            {
                string resultCardId = Encoding.Default.GetString(Format10Bytes(rebuffer, 8, 16));
                bool IsRegCardSuccess = BitConverter.ToBoolean(Format10Bytes(rebuffer, 24, 4), 0);
                int errorCode = BitConverter.ToInt32(Format10Bytes(rebuffer, 28, 4), 0);
                if (IsRegCardSuccess)
                {
                    message.SNetMsg_MessageType = "true|" + resultmsgId + "|" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "注册帐号成功|" + resultCardId;
                }
                else
                {
                    message.SNetMsg_MessageType = "false|" + resultmsgId + "|" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "注册帐号失败";
                    switch (errorCode)
                    {
                        case 1:
                            message.SNetMsg_MessageType += "|1|帐号重复";
                            break;
                        case 2:
                            message.SNetMsg_MessageType += "|2|帐号错误";
                            break;
                        case 3:
                            message.SNetMsg_MessageType += "|3|没有填写完全";
                            break;
                        case 4:
                            message.SNetMsg_MessageType += "|4|币种错误";
                            break;
                        case 5:
                            message.SNetMsg_MessageType += "|5|银行信息错误";
                            break;
                        case 6:
                            message.SNetMsg_MessageType += "|6|没有上级代理";
                            break;
                    }
                }
            }
            OnTimerStart();
        }
        #endregion

        #region 帐户存款业务
        /// <summary>
        /// 帐户存款业务
        /// </summary>
        /// <param name="model">存款信息实体类</param>
        public static void SNetMsg_IncCardPoint(tb_Order model, MessageTypeHandler message)
        {
            OnThreadStop();
            try
            {
                byte[] msgSize = BitConverter.GetBytes(116);
                byte[] msgId = BitConverter.GetBytes(10010);
                byte[] msgcardId = new byte[16];
                Encoding.UTF8.GetBytes(UTF8toGB2312(model.OUserName), 0, UTF8toGB2312(model.OUserName).Length, msgcardId, 0);
                byte[] msgmoneyType = new byte[4];
                Encoding.UTF8.GetBytes(model.OMoneyType, 0, model.OMoneyType.Length, msgmoneyType, 0);
                byte[] msgaddPoint = BitConverter.GetBytes(int.Parse((model.OAmount * 10).ToString()));//数据库 元  服务器  角
                byte[] msgorderId = new byte[40];
                Encoding.UTF8.GetBytes(model.OrderID, 0, model.OrderID.Length, msgorderId, 0);
                byte[] msgmemoText = new byte[40];
                Encoding.UTF8.GetBytes(UTF8toGB2312(model.OMemoText), 0, UTF8toGB2312(model.OMemoText).Length, msgmemoText, 0);
                IPAddress ipAddress = IPAddress.Parse(model.OPayIP);
                byte[] msgIP = ipAddress.GetAddressBytes();
                byte[] msgIpbytes = new byte[4];
                int p = 0;
                foreach (byte ip in msgIP)
                {
                    msgIpbytes[p] = ip;
                    p++;
                }
                byte[] msg = new byte[116];
                Buffer.BlockCopy(msgSize, 0, msg, 0, 4);
                Buffer.BlockCopy(msgId, 0, msg, 4, 4);
                Buffer.BlockCopy(msgcardId, 0, msg, 8, 16);
                Buffer.BlockCopy(msgmoneyType, 0, msg, 24, 4);
                Buffer.BlockCopy(msgaddPoint, 0, msg, 28, 4);
                Buffer.BlockCopy(msgorderId, 0, msg, 32, 40);
                Buffer.BlockCopy(msgmemoText, 0, msg, 72, 40);
                Buffer.BlockCopy(msgIpbytes, 0, msg, 112, 4);
                SendMsg(msg, 80, message);
                SNetMsg_OperCardPoint(message);
            }
            catch (SocketException ex)
            {
                message.SNetMsg_Boolean = false;
                message.SNetMsg_SocketException_MessageType = "1|10010|" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "账户存款失败,原因：" + ex.InnerException;
                Log.WriterLog(message.SNetMsg_SocketException_MessageType);  
            }
        }
        private static void SNetMsg_OperCardPoint(MessageTypeHandler message)
        {

            int resultmsgId = BitConverter.ToInt32(Format10Bytes(rebuffer, 4, 4), 0);
            if (resultmsgId == 10012)
            {
                string resultcartId = Encoding.Default.GetString(Format10Bytes(rebuffer, 8, 16));
                int resultoperType = BitConverter.ToInt32(Format10Bytes(rebuffer, 24, 4), 0);
                int resultoperPoint = BitConverter.ToInt32(Format10Bytes(rebuffer, 28, 4), 0);
                string resultorderId = Encoding.Default.GetString(Format10Bytes(rebuffer, 32, 40));
                bool resultoperSuccess = BitConverter.ToBoolean(Format10Bytes(rebuffer, 72, 4), 0);
                int resulterrorCode = BitConverter.ToInt32(Format10Bytes(rebuffer, 76, 4), 0);
                if (resultoperSuccess)
                {
                    if (resultoperType == 0)
                        message.SNetMsg_MessageType = "true|" + resultmsgId + "|" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "帐户充值成功|" + resultcartId.TrimEnd('\0') + "|" + resultorderId.TrimEnd('\0') + "|" + resultoperPoint.ToString().TrimEnd('\0');
                    else
                        message.SNetMsg_MessageType = "true|" + resultmsgId + "|" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "帐户提款成功|" + resultcartId.TrimEnd('\0') + "|" + resultorderId.TrimEnd('\0') + "|" + resultoperPoint.ToString().TrimEnd('\0');
                }
                else
                {
                    if (resultoperType == 0)
                        message.SNetMsg_MessageType = "false|" + resultmsgId + "|" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "帐户充值失败|" + resultcartId.TrimEnd('\0') + "|" + resultorderId.TrimEnd('\0') + "|" + resulterrorCode.ToString().TrimEnd('\0');
                    else
                        message.SNetMsg_MessageType = "false|" + resultmsgId + "|" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "帐户提款失败|" + resultcartId.TrimEnd('\0') + "|" + resulterrorCode.ToString().TrimEnd('\0');
                }
            }
            else
            { message.SNetMsg_Boolean = false; message.SNetMsg_SocketException_MessageType = "1|" + resultmsgId + "|" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "连接数据服务器出错"; }
            OnTimerStart();
        }

        #endregion

        #region 帐户取款业务
        /// <summary>
        /// 帐户取款 信息
        /// </summary>
        /// <param name="model">帐户取款实体类</param>
        public static void SNetMsg_DecCardPoint(tb_DrawInfo model, MessageTypeHandler message)
        {
            OnThreadStop();
            try
            {

                byte[] msgSize = BitConverter.GetBytes(244);
                byte[] msgId = BitConverter.GetBytes(10011);
                byte[] msgcardId = new byte[16];
                Gb2312.GetBytes(model.DcardId, 0, model.DcardId.Length, msgcardId, 0);
                byte[] msgcardName = new byte[16];
                Gb2312.GetBytes(UTF8toGB2312(model.DcardName), 0, UTF8toGB2312(model.DcardName).Length, msgcardName, 0);
                byte[] msgdrawPass = new byte[16];
                Gb2312.GetBytes(model.DdrawPass, 0, model.DdrawPass.Length, msgdrawPass, 0);
                byte[] msgmoneyType = new byte[4];
                Gb2312.GetBytes(model.DmoneyType, 0, model.DmoneyType.Length, msgmoneyType, 0);
                byte[] msgdrawPoint = BitConverter.GetBytes(model.DdrawPoint);
                byte[] msgbankCard = new byte[24];
                Gb2312.GetBytes(model.DbankCard, 0, model.DbankCard.Length, msgbankCard, 0);
                byte[] msgbankName = new byte[20];
                Gb2312.GetBytes(UTF8toGB2312(model.DbankName), 0, UTF8toGB2312(model.DbankName).Length, msgbankName, 0);
                byte[] msgorderId = new byte[24];
                Gb2312.GetBytes(UTF8toGB2312(model.DorderId), 0, UTF8toGB2312(model.DorderId).Length, msgorderId, 0);
                byte[] msgcodeId = new byte[16];
                Gb2312.GetBytes(model.DCodeId, 0, model.DCodeId.Length, msgcardId, 0);
                byte[] msgbankBC = new byte[16];
                byte[] msgbankCity = new byte[16];
                if (model.DmoneyType == "USD")
                {
                    Gb2312.GetBytes(UTF8toGB2312(model.DbankCity), 0, 16, msgbankBC, 0);
                    Gb2312.GetBytes(UTF8toGB2312(model.DbankCity), 0, (UTF8toGB2312(model.DbankCity).Length - 16), msgbankCity, 16);
                }
                else
                {
                    Gb2312.GetBytes(UTF8toGB2312(model.DbankBC), 0, UTF8toGB2312(model.DbankBC).Length, msgbankBC, 0);
                    Gb2312.GetBytes(UTF8toGB2312(model.DbankCity), 0, UTF8toGB2312(model.DbankCity).Length, msgbankCity, 0);
                }

                byte[] msgbankNameSub = new byte[20];
                Gb2312.GetBytes(UTF8toGB2312(model.DbankNameSub), 0, UTF8toGB2312(model.DbankNameSub).Length, msgbankNameSub, 0);
                byte[] msgmemoText = new byte[40];
                Gb2312.GetBytes(UTF8toGB2312(model.DmemoText), 0, UTF8toGB2312(model.DmemoText).Length, msgmemoText, 0);
                IPAddress ipAddress = IPAddress.Parse(model.DIP);
                byte[] msgIP = ipAddress.GetAddressBytes();
                byte[] msg = new byte[244];
                Buffer.BlockCopy(msgSize, 0, msg, 0, 4);
                Buffer.BlockCopy(msgId, 0, msg, 4, 4);
                Buffer.BlockCopy(msgcardId, 0, msg, 8, 16);
                Buffer.BlockCopy(msgcardName, 0, msg, 24, 16);
                Buffer.BlockCopy(msgdrawPass, 0, msg, 40, 16);
                Buffer.BlockCopy(msgmoneyType, 0, msg, 56, 4);
                Buffer.BlockCopy(msgdrawPoint, 0, msg, 60, 4);
                Buffer.BlockCopy(msgbankCard, 0, msg, 64, 24);
                Buffer.BlockCopy(msgbankName, 0, msg, 88, 20);
                Buffer.BlockCopy(msgorderId, 0, msg, 108, 24);
                Buffer.BlockCopy(msgcodeId, 0, msg, 132, 16);
                Buffer.BlockCopy(msgbankBC, 0, msg, 148, 16);
                Buffer.BlockCopy(msgbankCity, 0, msg, 164, 16);
                Buffer.BlockCopy(msgbankNameSub, 0, msg, 180, 20);
                Buffer.BlockCopy(msgmemoText, 0, msg, 200, 40);
                Buffer.BlockCopy(msgIP, 0, msg, 240, 4);
                SendMsg(msg, 80, message);
                SNetMsg_OperCardPoint(message);
            }
            catch (SocketException ex)
            {
                message.SNetMsg_Boolean = false;
                message.SNetMsg_SocketException_MessageType = "1|10011|" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "账户提款失败,原因：" + ex.InnerException;
                Log.WriterLog(message.SNetMsg_SocketException_MessageType);  
            }
        }
        #endregion

        #region 帳戶取款用戶名密碼驗證及密碼返回
        /// <summary>
        /// 帳戶取款用戶名密碼驗證及密碼返回
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="userPwd"></param>
        /// <param name="message"></param>
        public static void SNetMsg_GetCardCreditLeft(string userName, string userPwd, string moneyChip, MessageTypeHandler message)
        {
            OnThreadStop();
            try
            {
                if (userName.Trim() != "" && userPwd != "")
                {
                    byte[] msgSize = BitConverter.GetBytes((int)44);
                    byte[] msgId = BitConverter.GetBytes((int)10013);
                    byte[] msguserName = new byte[16];
                    Encoding.Default.GetBytes(userName, 0, userName.Length, msguserName, 0);
                    byte[] msgusrPwd = new byte[16];
                    Encoding.Default.GetBytes(userPwd, 0, userPwd.Length, msgusrPwd, 0);
                    byte[] msgmoneyChip = new byte[4];
                    Encoding.Default.GetBytes(moneyChip, 0, moneyChip.Length, msgmoneyChip, 0);
                    byte[] msg = new byte[44];
                    Buffer.BlockCopy(msgSize, 0, msg, 0, 4);
                    Buffer.BlockCopy(msgId, 0, msg, 4, 4);
                    Buffer.BlockCopy(msguserName, 0, msg, 8, 16);
                    Buffer.BlockCopy(msgusrPwd, 0, msg, 24, 16);
                    Buffer.BlockCopy(msgmoneyChip, 0, msg, 40, 4);
                    SendMsg(msg, 32, message);
                    SNetMsg_CardCreditLeftResult(message);
                }
                else
                {
                    message.SNetMsg_Boolean = false;
                    message.SNetMsg_SocketException_MessageType = "false|10013|" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "用戶名密碼不能為空";
                }
            }
            catch (SocketException ex)
            {
                message.SNetMsg_Boolean = false;
                message.SNetMsg_SocketException_MessageType = "1|10013|" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "帳戶取款用戶名密碼驗證及密碼错误,原因：" + ex.InnerException;
                Log.WriterLog(message.SNetMsg_SocketException_MessageType);
            }
        }
        

        public static void SNetMsg_CardCreditLeftResult(MessageTypeHandler message)
        {
            if (rebuffer != null)
            {
                int resultId = BitConverter.ToInt32(Format10Bytes(rebuffer, 4, 4), 0);
                string resultCartId = Encoding.Default.GetString(Format10Bytes(rebuffer, 8, 16));
                bool resultbSuccess = BitConverter.ToBoolean(Format10Bytes(rebuffer, 24, 4), 0);
                int resultcodeResult = BitConverter.ToInt32(Format10Bytes(rebuffer, 28, 4), 0);
                if (resultId == 10014)
                {
                    if (resultbSuccess)
                    {
                        message.SNetMsg_Boolean = true;
                        message.SNetMsg_MessageType = "true|" + resultId + "|" + resultcodeResult;
                    }
                    else
                    {
                        message.SNetMsg_MessageType = "false|" + resultId + "|" + resultcodeResult;
                    }
                }
            }
            else
            { message.SNetMsg_SocketException_MessageType = "false|沒回複"; }
            OnTimerStart();
        }
        #endregion
    }
}